
class IrbOperation  < Operation


	condition do |context|
		args = context.symbol(:args)
		args.find{|x| x == "--irb"}
	end


	def run(context)

		require "irb"

		logger.debug Locale.irb_spawn

		ap_path = nil

		#$0 = File::basename(ap_path, ".rb") if ap_path

		self.hook_argv( "" ) do
			IRB.setup(ap_path)
		end

		irb = IRB::Irb.new

		IRB.instance_eval do
		@CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
		@CONF[:MAIN_CONTEXT] = irb.context
		end
 
		trap("SIGINT") do
		irb.signal_handle
		end
		    
		catch(:IRB_EXIT) do
		irb.eval_input
		end


		rescue Rsif::RsifLoadError => e

		logger.error Locale.irb_spawn_load_error

		rescue Exception => e

		logger.error Locale.irb_spawn_generic_error

	end

	class << self
		attr_accessor	:new_values
		attr_accessor	:original_argv_shift
	end
protected
	def hook_argv( new_values)

		IrbOperation.original_argv_shift = ARGV.method(:shift)
		IrbOperation.new_values = new_values
		def ARGV.shift
			IrbOperation.new_values
		end

		begin
			yield
		ensure
			def ARGV.shift
				IrbOperation.original_argv_shift.call
			end
		end
	end

end
